package com.cdie.myschool.service.impl;

import com.cdie.myschool.common.R;
import com.cdie.myschool.domain.dto.TeacherAwardsDTO;
import com.cdie.myschool.domain.entity.Awards;
import com.cdie.myschool.domain.entity.AwardsOld;
import com.cdie.myschool.domain.entity.Calendar;
import com.cdie.myschool.domain.vo.TeacherAwardsVO;
import com.cdie.myschool.mapper.TeacherAwardsMapper.TeacherAwardsMapper;
import com.cdie.myschool.mapper.TeacherAwardsMapper.TeacherAwardsOldMapper;
import com.cdie.myschool.service.TeacherAwardsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 雷金臣199000347
 * @date 2022/10/6 - 8:52
 */
@Service
public class TeacherAwardsServiceImpl implements TeacherAwardsService {
    @Autowired
    private TeacherAwardsOldMapper teacherAwardsOldMapper;

    @Autowired
    private TeacherAwardsMapper teacherAwardsMapper;

    @Override
    public R<Awards> getAwards() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<AwardsOld> awardsOldList=teacherAwardsOldMapper.TeacherAwardsInfo();
        for (int i = 0; i <awardsOldList.size() ; i++) {
            Awards awards=new Awards();
            awards.setTeacherId(awardsOldList.get(i).getGh());
            awards.setAwardsDay(awardsOldList.get(i).getHjrq());
            awards.setAwardsName(awardsOldList.get(i).getJlmc());
            String t1=awardsOldList.get(i).getHjrq();//获奖日期  20210801000000
            long a= Long.valueOf(t1).longValue();//20210801000000
            //学年学期id
            String t2=t1.substring(0,4);//年2021
//            String t3=t1.substring(0,8);//月日20210801
            Calendar calendar=teacherAwardsMapper.selectCalendar(t2,"0");//第一学期0
            String t4=formatter.format(calendar.getBeginTime());//线数据库时间2009-02-22 16:00:00
            String t5=t4.replace("-","");//20090222 16:00:00
            String t6=t5.replace(" ","");//2009022216:00:00
            String t7=t6.replace(":","");//20090222160000
            long b=Long.valueOf(t7).longValue();
            if(a<b){
//                long year=Long.valueOf(t2).longValue()-1;
                String schoolYear=String.valueOf(Long.valueOf(t2).longValue()-1);
                Calendar calendar2=teacherAwardsMapper.selectCalendar(schoolYear,"1");
                awards.setSemesterId(calendar2.getSemesterId());
//                list.get(i).setSemesterId(calendar2.getSemesterId());
            }else {
                String tl4=formatter.format(calendar.getEndTime());
                String tl5=tl4.replace("-","");//20090222 16:00:00
                String tl6=tl5.replace(" ","");//2009022216:00:00
                String tl7=tl6.replace(":","");//20090222160000
                long c=Long.valueOf(tl7).longValue();
                if(a>=b&&a<=c){
                    Calendar calendar4=teacherAwardsMapper.selectCalendar(t2,"1");
                    awards.setSemesterId(calendar4.getSemesterId());
//                    list.get(i).setSemesterId(calendar4.getSemesterId());
                }
                if(a>c){
                    String schoolYear=String.valueOf(Long.valueOf(t2).longValue()+1);
                    Calendar calendar5=teacherAwardsMapper.selectCalendar(schoolYear,"0");
                    awards.setSemesterId(calendar5.getSemesterId());
//                    list.get(i).setSemesterId(calendar5.getSemesterId());
                }
            }
            awards.setTimestamp(new Date());
            int result=teacherAwardsMapper.InsertTeacherAwards(awards);
            if(result!=1){
                return R.Failed("awards表插入失败！");
            }
        }

        return R.Success("awards表插入成功！");
        }

    @Override
    public R<TeacherAwardsVO> getAwardsInfo(TeacherAwardsDTO teacherAwardsDTO) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TeacherAwardsVO teacherAwardsVO=new TeacherAwardsVO();
        Date date=new Date();//
        String pattern = "yyyy-MM-dd HH:mm:ss";
        String pattern2 ="2018-09-03 16:00:00";//2018-09-03 16:00:00同年1学期  2019-01-17 16:00:00  同年2学期  上一年2学期2018-07-05 16:00:00
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern2);
        String dateString=simpleDateFormat.format(date);
        String ti=dateString.replace(" ","");
        String ti2=ti.replace(":","");
        String time=ti2.replace("-","");
        long a= Long.valueOf(time).longValue();
        String t2=time.substring(0,4);
        Calendar calendarB=teacherAwardsMapper.selectCalendar(t2,"0");//第一学期0
        String t4= formatter.format(calendarB.getBeginTime());//线数据库时间2009-02-22 16:00:00
        String t5=t4.replace("-","");//20090222 16:00:00
        String t6=t5.replace(" ","");//2009022216:00:00
        String t7=t6.replace(":","");//20090222160000
        long bb=Long.valueOf(t7).longValue();
        Calendar calendarE=teacherAwardsMapper.selectCalendar(t2,"1");//第2学期1
        String te4=formatter.format(calendarE.getBeginTime());//线数据库时间2009-02-22 16:00:00
        String te5=te4.replace("-","");//20090222 16:00:00
        String te6=te5.replace(" ","");//2009022216:00:00
        String te7=te6.replace(":","");//20090222160000
        long be=Long.valueOf(te7).longValue();
        if(a<bb){
            //上一年的第二学期
            System.out.println("测试a小于b");
            String schoolYear=String.valueOf(Long.valueOf(t2).longValue()-1);
            Calendar calendar2=teacherAwardsMapper.selectCalendar(schoolYear,"1");
            long SemesterId= calendar2.getSemesterId();
            String semesterId=Long.toString(SemesterId);
            teacherAwardsDTO.setSemesterId(semesterId);
        }else {
            if(a>=bb&&a<=be){
                System.out.println("测试a大于等于b且小于等于c");
                Calendar calendar4=teacherAwardsMapper.selectCalendar(t2,"0");
                long SemesterId=calendar4.getSemesterId();
                String semesterId=Long.toString(SemesterId);
                System.out.println("测试aaaaa"+SemesterId);
                System.out.println("测试bbbbb"+t2+"1");
                System.out.println("测试ccccc"+calendar4.getSchoolYear());
                System.out.println("测试ddddd"+calendar4.getSemester());
                teacherAwardsDTO.setSemesterId(semesterId);
                System.out.println("测试eeeee"+teacherAwardsDTO.getSemesterId());
                long y=Long.valueOf(t2).longValue()+1;
                String schoolYear2=Long.toString(y);
            }
            if(a>be){
                System.out.println("测试a大于c");
                long y=Long.valueOf(t2).longValue()+1;
                String ye=Long.toString(y);
                Calendar calendar5=teacherAwardsMapper.selectCalendar(ye,"0");
                long SemesterId=calendar5.getSemesterId();
                String semesterId=Long.toString(SemesterId);
                teacherAwardsDTO.setSemesterId(semesterId);
            }
        }
        List<Awards> awardsList=teacherAwardsMapper.getAwardsInfo(teacherAwardsDTO);
        teacherAwardsVO.setAwardsNum(awardsList.size());
        List awardsName=new ArrayList();
        for (int i = 0; i < awardsList.size(); i++) {
            String awards=awardsList.get(i).getAwardsName();
            System.out.println("测试mmmmm"+awards);
            awardsName.add(awards);
        }
        teacherAwardsVO.setAwardsList(awardsName);
        System.out.println("测试nnnnn"+teacherAwardsVO);
        return R.Success("获奖信息查询成功！",teacherAwardsVO);
    }

}
